package com.yra.controller.mall;

import com.yra.common.Constants;
import com.yra.common.ServiceResultEnum;
import com.yra.controller.common.ValidateController;
import com.yra.domain.ImageCode;
import com.yra.service.MallUserService;
import com.yra.util.MD5Util;
import com.yra.util.Result;
import com.yra.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: yrq
 * @Date: Create in 15:21 2019/12/12
 */
@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
    private MallUserService mallUserService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ModelAndView goToLogin(){
        return new ModelAndView("/mall/login");
    }

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public ModelAndView goToRegister(){
        return new ModelAndView("/mall/register");
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ResponseEntity<Boolean> register(@RequestParam Map<String , Object> params){
        System.out.println(params.toString());
        return ResponseEntity.status(HttpStatus.OK).body(mallUserService.insertMallUser(params));
//        return ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseEntity<Result> login(@RequestParam("userName") String userName,
                                        @RequestParam("verifyCode") String verifyCode,
                                        @RequestParam("password") String password,
                                        HttpSession httpSession,
                                        HttpSessionSessionStrategy sessionStrategy,
                                        ServletWebRequest servletWebRequest){
        ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        System.out.println(codeInSession.getCode());
        System.out.println(userName);
        System.out.println(verifyCode);
        System.out.println(password);
        if (StringUtils.isEmpty(userName)) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_NULL.getResult()));
        }
        if (StringUtils.isEmpty(password)) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_NULL.getResult()));
        }
        if (StringUtils.isEmpty(verifyCode)) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_NULL.getResult()));
        }
        if (StringUtils.isEmpty(codeInSession) || !verifyCode.equals(codeInSession.getCode())) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult()));
        }
        //todo 清verifyCode
        Boolean loginFlag = mallUserService.login(userName, MD5Util.MD5Encode(password, "UTF-8"));
        //登录成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(loginFlag)) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genSuccessResult());
        }else {
            //登录失败
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_ERROR.getResult()));
        }
    }
}
